﻿using Nebukam.Common.Editor;
using System.Collections.Generic;
using UnityEngine;



public static class DrawTools
{

    public static void DrawCapsule(CapsuleCollider cc, Color color)
    {
        Vector3 start = Vector3.zero;
        Vector3 end = Vector3.zero;
        switch (cc.direction)
        {
            case 0:
                start = cc.center + cc.gameObject.transform.right * cc.height / 2;
                end = cc.center - cc.gameObject.transform.right * cc.height / 2;
                break;
            case 1:
                start = cc.center + cc.gameObject.transform.up * cc.height / 2;
                end = cc.center - cc.gameObject.transform.up * cc.height / 2;
                break;
            case 2:
                start = cc.center + cc.gameObject.transform.forward * cc.height / 2;
                end = cc.center - cc.gameObject.transform.forward * cc.height / 2;
                break;
        }
        //Gizmos.DrawCapsule(start, end, color, cc.radius);
    }

    public static void DrawEllipse(Vector3 center, Vector3 dir, float fMinRadius, float fMaxRadius, float fH = 1)
    {
        fH = fH / 2;
        List<Vector3> list = new List<Vector3>();
        float angle = 0;
        Vector3 start = center - dir * fMinRadius;
        Vector3 end = center + dir * fMinRadius;
        float fBigRadius = fMaxRadius * 2;// fMinRadius;
        int nTime = 0;
        while (nTime < 2)
        {
            for (int i = 0; i <= 60; i++)
            {
                Vector3 p = new Vector3();
                Vector3 dir2 = CommonTools.RotateVector(dir, Vector3.up, angle);
                dir2.Normalize();
                Vector3 r = start + dir2 * fBigRadius; // 得到中心到大圆上的点
                float fToEndDis = Vector3.Distance(end, r) / 2; // 获取到另一个焦点的距离
                Vector3 R1 = (start - r);
                Vector3 R2 = (end - r);
                float fRAngel = CommonTools.GetAnglePlane(R1, R2);
                float l;
                if (fRAngel * Mathf.Deg2Rad == 0)
                {
                    l = fToEndDis;
                }
                else if (fRAngel * Mathf.Deg2Rad == Mathf.PI)
                {
                    l = fToEndDis;
                }
                else
                    l = fToEndDis / Mathf.Cos(fRAngel * Mathf.Deg2Rad);

                R1.Normalize();
                p = r + R1 * l;
                p.y += fH;
                //p = RotateRound(p, center, Vector3.up, -off - 90);
                var self = CommonTools.GetDistance(p, start);
                var other = CommonTools.GetDistance(p, end);
                if (self < other)
                    list.Add(p);
                angle += 6;
            }
            start = center + dir * fMinRadius;
            end = center - dir * fMinRadius;
            dir = -dir;
            ++nTime;
        }
        int j = 0;
        Vector3 s, s1;
        for (j = 0; j < list.Count - 1; ++j)
        {
            Gizmos.DrawLine(list[j], list[j + 1]);
            s = list[j];
            s1 = list[j + 1];
            s.y = 0;
            s1.y = 0;
            Gizmos.DrawLine(s, s1);
            Gizmos.DrawLine(s, list[j]);
        }

        s = list[j];
        s1 = list[0];
        s.y = 0;
        s1.y = 0;
        Gizmos.DrawLine(list[j], list[0]);
        Gizmos.DrawLine(s, list[j]);
        var c = center + dir * fMinRadius;
        Gizmos.DrawLine(c, c + Vector3.up * 4);
        c = center - dir * fMinRadius;
        Gizmos.DrawLine(c, c + Vector3.up * 4);
    }
    public static void DrawPillar(Vector3 center, float fRadius, float fMin = 0, float fH = 1)
    {
        Vector3 dir = Vector3.forward;
        Vector3 pos = center + dir * fRadius;
        Vector3 pos2 = pos;
        for (int i = 0; i < 36; ++i)
        {
            dir = CommonTools.RotateVector(dir, Vector3.up, 10);
            pos2 = center + dir * fRadius;
            //if (pos2.y > 0)
            //    pos2.y = 1;
            //else
            //pos2.y += fH;
            Gizmos.DrawLine(pos, pos2);

            var p1 = pos;
            var p2 = pos2;
            p1.y = center.y + fH;
            p2.y = center.y + fH;
            Gizmos.DrawLine(p1, p2);

            Gizmos.DrawLine(pos, p1);

            pos = pos2;
            ///pos2 = pos4;
        }
    }
    //用Gizmos画立体方块
    public static void DrawBall(Vector3 center, float fRadius)
    {
        Gizmos.DrawWireSphere(center, fRadius);
    }

    public static void DrawBox2d(Rect rect, Color color)
    {
        Vector3 lt = new Vector2(rect.x, rect.yMax);
        Vector3 rb = new Vector2(rect.xMax, rect.y);
        Vector3 rt = rect.max;
        Vector3 lb = rect.min;
        Draw.Line(lt, rt, color);
        Draw.Line(lt, lb, color);
        Draw.Line(rb, rt, color);
        Draw.Line(rb, lb, color);

    }

    public static void DrawBox2d(Vector2 center, float w, float h)
    {
        Vector2 lt = new Vector2(center.x - w / 2, center.y + h / 2);
        Vector2 rt = new Vector2(center.x + w / 2, center.y + h / 2);
        Vector2 lb = new Vector2(center.x - w / 2, center.y - h / 2);
        Vector2 rb = new Vector2(center.x + w / 2, center.y - h / 2);
        Gizmos.DrawLine(lt, rt);
        Gizmos.DrawLine(lt, lb);
        Gizmos.DrawLine(rb, rt);
        Gizmos.DrawLine(rb, lb);
    }
    public static void DrawBox(Vector3 center, Vector3 dir, float w, float l, float h)
    {
        float ww = w / 2;
        float ll = h;
        float hh = l / 2;
        Vector3 max = CommonTools.MoveVector(center, dir, new Vector3(ww, ll, hh));
        Vector3 max1 = CommonTools.MoveVector(center, dir, new Vector3(-ww, ll, hh));
        Vector3 max2 = CommonTools.MoveVector(center, dir, new Vector3(ww, 0, hh));
        Vector3 max3 = CommonTools.MoveVector(center, dir, new Vector3(ww, ll, -hh));

        Vector3 min = CommonTools.MoveVector(center, dir, new Vector3(-ww, 0, -hh));
        Vector3 min1 = CommonTools.MoveVector(center, dir, new Vector3(ww, 0, -hh));
        Vector3 min2 = CommonTools.MoveVector(center, dir, new Vector3(-ww, ll, -hh));
        Vector3 min3 = CommonTools.MoveVector(center, dir, new Vector3(-ww, 0, hh));

        Gizmos.DrawLine(min, min1);
        Gizmos.DrawLine(min, min2);
        Gizmos.DrawLine(min, min3);
        Gizmos.DrawLine(max, max1);
        Gizmos.DrawLine(max, max2);
        Gizmos.DrawLine(max, max3);

        Gizmos.DrawLine(max1, min2);
        Gizmos.DrawLine(max1, min3);
        Gizmos.DrawLine(max2, min1);
        Gizmos.DrawLine(max2, min3);
        Gizmos.DrawLine(max3, min2);
        Gizmos.DrawLine(max3, min1);

    }
    public static void DrawRay(Vector3 start, Vector3 dir)
    {
        Gizmos.DrawLine(start, start + dir * 100000);
    }
    public static void DrawLine(Vector3 start, Vector3 end, Color color, float h = 0, bool bDrawHeight = true)
    {
        Gizmos.color = color;
        Vector3 start2 = start;
        start2.y += h;
        Vector3 end2 = end;
        end2.y += h;
        Gizmos.DrawLine(start, end);
        Gizmos.DrawLine(start2, end2);
        if (bDrawHeight)
        {
            // 设置颜色
            Gizmos.DrawLine(start, start2);
            Gizmos.DrawLine(end, end2);
        }
    }
    public static void DrawSector(Vector3 pos, float startDir, Vector3 dir, Color color, float fAngel, float fMinRaius, float fMaxRadius, float h = 1)
    {
        float fCur = -fAngel / 2;
        float fOffset = fAngel / 8;
        var newdir = CommonTools.RotateVector(dir, Vector3.up, startDir);
        newdir = CommonTools.RotateVector(newdir, Vector3.up, fCur);
        var end = pos + newdir * fMaxRadius;
        var start = pos + newdir * fMinRaius;
        DrawLine(start, end, color, -pos.y, true);
        for (int i = 0; i < 8; ++i)
        {
            newdir = CommonTools.RotateVector(newdir, Vector3.up, fOffset);
            var nextend = pos + newdir * fMaxRadius;
            var nextstart = pos + newdir * fMinRaius;
            if (fMinRaius > 0)
            {
                DrawLine(start, nextstart, color);
            }
            DrawLine(end, nextend, color, h, true);
            DrawLine(nextend, nextstart, color, h, true);
            start = nextstart;
            end = nextend;
        }
    }
    static public bool IsFlagMask(int value, int flag)
    {
        return (value & flag) == flag;
    }


}
